surface.CreateFont( "Fixedsys", 15, 400, true, false, "CosmicDisplay" );

function C.GetTextWidth( str, font )
	
	surface.SetFont( font );
	local w, _ = surface.GetTextSize( str );
	return w;
	
end

function C.GetTextHeight( str, font )
	
	surface.SetFont( font );
	local _, h = surface.GetTextSize( str );
	return h;
	
end

function GM:HUDShouldDraw( element )
	
	if( element == "CHudDamageIndicator" ) then return false end
	return true;
	
end

function GM:HUDPaint()
	
	self.BaseClass:HUDPaint();
	
	C.DrawNames();
	
	local planetid = C.InPlanet( LocalPlayer() );
	
	local temp = C.GetTemp( LocalPlayer() ) - 273;
	local tempcol = Color( 255, 255, 255, 255 );
	
	local grav = 0;
	local gravcol = Color( 255, 255, 255, 255 );
	
	if( planetid > 0 ) then
		
		grav = math.Round( C.MapObjects.Planets[planetid].gravity * 100 ) / 100;
		
	end
	
	local oxy = ( C.HasOxy( LocalPlayer() ) and "Yes" or "No" );
	local oxycol = Color( 255, 255, 255, 255 );
	
	if( !C.HasTemp( LocalPlayer() ) ) then
		
		if( C.Burning( LocalPlayer() ) ) then
			
			tempcol = Color( 255, 0, 0, 255 );
			
		else
			
			tempcol = Color( 128, 128, 255, 255 );
			
		end
		
	end
	
	if( !C.HasOxy( LocalPlayer() ) ) then
		
		oxycol = Color( 255, 0, 0, 255 );
		
	end
	
	local wide = C.GetTextWidth( "Temperature: -000C", "CosmicDisplay" ) + 20;
	
	local h = 65;
	
	if( C.GetSuitTime( LocalPlayer() ) < 300 ) then
		
		h = 80;
		
	end
	
	draw.RoundedBox( 10, ( ScrW() / 2 ) - ( wide / 2 ), 0, wide, h, Color( 0, 0, 0, 150 ) );
	
	draw.DrawText( "Temperature: " .. tostring( temp ) .. "C", "CosmicDisplay", ScrW() / 2, 10, tempcol, 1 );
	draw.DrawText( "Oxygen: " .. oxy, "CosmicDisplay", ScrW() / 2, 25, oxycol, 1 );
	draw.DrawText( "Gravity: " .. tostring( grav ) .. "G", "CosmicDisplay", ScrW() / 2, 40, gravcol, 1 );
	
	if( C.GetSuitTime( LocalPlayer() ) < 300 ) then
		
		local suitcol = Color( 255, 255, 255, 255 );
		
		if( C.GetSuitTime( LocalPlayer() ) <= 30 ) then
			
			suitcol = Color( 255, 0, 0, 255 );
			
		end
		
		draw.DrawText( "Suit: " .. string.ToMinutesSecondsMilliseconds( C.GetSuitTime( LocalPlayer() ) ), "CosmicDisplay", ScrW() / 2, 70, suitcol, 1 );
		
	end
	
	local cash = C.Credits( LocalPlayer() );
	local cashcol = Color( 255, 255, 255, 255 );
	
	if( cash == 0 ) then
		
		cashcol = Color( 255, 0, 0, 255 );
		
	end
	
	local wide = C.GetTextWidth( "Credits: 000000", "CosmicDisplay" ) + 20;
	draw.RoundedBox( 10, ( ScrW() / 2 ) - ( wide / 2 ), ScrH() - 50, wide, 45, Color( 0, 0, 0, 150 ) );
	draw.DrawText( "Credits: " .. tostring( cash ), "CosmicDisplay", ScrW() / 2, ScrH() - 50 + 15, cashcol, 1 );
	
end

function C.DrawEntityBox( pos, text, onstate, r, g, b )
	
	local dist = LocalPlayer():GetPos():Distance( pos );
	
	local alph = 0;
	
	if( dist < 300 ) then
		
		alph = 1.5;
		
	elseif( dist < 400 ) then
		
		local n = dist - 300;
		local rec = 100 - n;
		alph = rec / 100;
		
	end
	
	local col = Color( 255, 0, 0, alph * 100 );
	
	if( onstate ) then
		
		col = Color( 0, 255, 0, alph * 100 );
		
	end
	
	if( r ) then
		
		col = Color( r, g, b, alph * 100 );
		
	end
	
	if( alph > 0 ) then
		
		local spos = pos:ToScreen();
		
		if( spos.visible ) then
			
			draw.RoundedBox( 10, spos.x, spos.y, C.GetTextWidth( text, "CosmicDisplay" ) + 10, C.GetTextHeight( text, "CosmicDisplay" ) + 10 - 15, col );
			draw.DrawText( text, "CosmicDisplay", spos.x + 5, spos.y + 5, Color( 255, 255, 255, alph * 255 ), 0 );
			
		end
		
	end
	
end

local ResourceTranslateTab = {
	"Oxygen",
	"Coolant",
	"Energy"
};

local ResourceTranslateTabRate = {
	"Oxygen/s",
	"Coolant/s",
	"Energy/s"
};

function C.DrawNames()
	
	for _, v in pairs( ents.GetAll() ) do -- Alternative??
		
		if( v.Cosmic ) then
			
			local pos = v:GetPos();
			
			if( v.CosmicType == CE_GENERATOR ) then
				
				local persec = tostring( v.Amount * ( 1 / v.Delay ) );
				local text = v.PrintName .. "\n" .. persec .. " " .. ResourceTranslateTabRate[v.Resource] .. "\n";
				
				if( v.Required ) then
					
					for k, n in pairs( v.Required ) do
						
						local persec = tostring( -n * ( 1 / v.Delay ) );
						text = text .. persec .. " " .. ResourceTranslateTabRate[k] .. "\n";
						
					end
					
				end
				
				local bc = v:GetNWBool( "O" );
				
				if( v.AlwaysOn and v:CanGenerate() ) then
					
					bc = true;
					
				end
				
				C.DrawEntityBox( pos, text, bc );
				
			elseif( v.CosmicType == CE_SCRUBBER ) then
				
				local text = v.PrintName .. "\n";
				C.DrawEntityBox( pos, text, v:GetNWBool( "O" ) );
				
			elseif( v.CosmicType == CE_CONTAINER ) then
				
				local text = v.PrintName .. "\n";
				
				for k, n in pairs( v.Resources ) do
					
					local amt = v:GetNWInt( tostring( n ) );
					local max = v:GetNWInt( "M" .. tostring( n ) );
					
					text = text .. ResourceTranslateTab[n] .. ": " .. tostring( amt ) .. "/" .. tostring( max ) .. " (" .. tostring( math.Round( 100 * ( amt / max ) ) ) .. "%)\n";
					
				end
				
				C.DrawEntityBox( pos, text, false, 0, 148, 255 );
				
			elseif( v.CosmicType == CE_DISTRIBUTOR or v.CosmicType == CE_BIGDISTRIBUTOR ) then
				
				local text = v.PrintName .. "\n";
				
				for k, n in pairs( v.Resources ) do
					
					local persec = tostring( n * ( 1 / v.Delay ) );
					
					text = text .. ResourceTranslateTab[k] .. " Cost: " .. persec .. "/s\n";
					
				end
				
				C.DrawEntityBox( pos, text, v:GetNWBool( "O" ) );
				
			elseif( v.CosmicType == CE_SUIT ) then
				
				C.DrawEntityBox( pos, "Suit Distributor\n", false, 110, 0, 255 );
				
			elseif( v.CosmicType == CE_TERMINAL ) then
				
				C.DrawEntityBox( pos, v.PrintName .. "\n", false, 255, 72, 0 );
				
			end
			
		end
		
	end
	
end